define(function(require, exports) {

    var app = {};
    require('lightbox');

    var router = require('router');
    var query = require('model/query');

    app.pageNum = 1;

    app.init = function() {
        router.init();
        app.bind();
    };

    app.search = function($content, $keyword, $add) {
        var opts = {
            keyword: $keyword.val()
        };
        router.state = 'search';
        $add ? (opts.pageNum = app.pageNum++) : (app.pageNum = 0);
        query.search(opts, function(data) {
            var imgList = data.list;
            //change with the .photo width percentage
            var imgWidth = $content.width() / 4;
            var imgHTML = '';
            var ratio = 1;
            var height = 0;
            $.each(imgList, function(idx, item) {
                ratio = parseInt(item.height) / parseInt(item.width);
                height = parseInt(ratio * imgWidth);
                imgHTML += '<a href="' + item.img + '" style="height:' + height + 'px;" class="photo" data-lightbox="searchPhoto">\
                                <img src="' + item.img + '"/>\
                            </a>';
            });
            var elems = $(imgHTML);
            if (!$add) {
                $content.html(elems).masonry('prepended', elems);
            } else {
                $content.append(elems).masonry('appended', elems);
            }
        });
    };

    app.bind = function() {
        //nav-bar click effect
        $('.navbar-nav > li').click(function() {
            $('li.active').removeClass('active');
            $(this).addClass('active');
        });

        //search focus effect
        $('.input-group').on('focus', '.form-control', function() {
            $(this).closest('.input-group, .form-group').addClass('focus');
        }).on('blur', '.form-control', function() {
            $(this).closest('.input-group, .form-group').removeClass('focus');
        });

        var $keyword = $('#keyword');
        var $content = $('#content');

        !Masonry && require('masonry');

        $content.masonry({
            itemSelector: '.photo'
        });

        // hide #back-top first
        $("#back-top").hide();

        // scroll body to 0px on click
        $('#back-top a').click(function () {
            $('body,html').animate({
                scrollTop: 0
            }, 800);
            return false;
        });

        $(window).scroll(function(e){
            if ('search' === router.state) {
                var contentHeight = $content.height();
                var bufferHeight = 80;
                var $window = $(window);
                if ($window.scrollTop() + $window.height() - bufferHeight >= contentHeight) {
                    app.search($content, $keyword, true);
                }
            }
            if ($(this).scrollTop() > 100) {
                $('#back-top').fadeIn();
            } else {
                $('#back-top').fadeOut();
            }
        });

        $keyword.keyup(function(e) {
            if (13 === e.keyCode) {
                app.search($content, $keyword);
            }
        });

        $('#search').click(function() {
            app.search($content, $keyword);
        });

        // 这行是 Opera 的补丁, 少了它 Opera 是直接用跳的而且画面闪烁 by willin
        $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
        $('#top').click(function(){
            $body.animate({scrollTop: $('#header').offset().top}, 1000);
            return false;// 返回false可以避免在原链接后加上#
        });
    };

    return {
        init: app.init
    };
});
